package com.oss.service.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.common.BaseResponse;
import com.oss.dto.dictionary.DictionaryTypeAddDto;
import com.oss.dto.dictionary.DictionaryTypeDeleteDto;
import com.oss.dto.dictionary.DictionaryTypePageDto;
import com.oss.dto.dictionary.DictionaryTypeUpdateDto;
import com.oss.entity.system.Dictionary;
import com.oss.entity.system.DictionaryType;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.system.DictionaryMapper;
import com.oss.mapper.system.DictionaryTypeMapper;
import com.oss.util.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chengqiang
 */
@Service
public class DictionaryTypeService extends ServiceImpl<DictionaryTypeMapper, DictionaryType> {
    @Autowired
    DictionaryTypeMapper dictionaryTypeMapper;
    @Autowired
    DictionaryMapper dictionaryMapper;

    /**
     * 新增字典类型
     * 字典类型code不能为空
     * @param dto 入参
     * @return 返回
     */
    public BaseResponse<String> addDictionaryType(DictionaryTypeAddDto dto) {
        LambdaQueryWrapper<DictionaryType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DictionaryType::getIsDeleted, IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.eq(DictionaryType::getDicTypeCode, dto.getDicTypeCode());
        long count = dictionaryTypeMapper.selectCount(lambdaQueryWrapper);
        if (count > 0){
            return BaseResponse.error("字典类型code已存在, 不能重复添加");
        }
        DictionaryType entity = new DictionaryType();
        BeanUtils.copyProperties(dto,entity);
        entity.setDicTypeId(SnowFlakeUtil.getID());
        dictionaryTypeMapper.insert(entity);
        return BaseResponse.success("操作成功");
    }

    /**
     * 删除字典类型
     * 联动删除字典类型下每一个字典值
     * @param dto 入参
     * @return 返回操作提示信息
     */
    public String deleteDictionaryType(DictionaryTypeDeleteDto dto) {
        // 1. 删除字典类型本身
        LambdaUpdateWrapper<DictionaryType> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DictionaryType::getIsDeleted, IsDeletedEnum.NO.getFlag());
        lambdaUpdateWrapper.set(DictionaryType::getIsDeleted, IsDeletedEnum.YES.getFlag());
        lambdaUpdateWrapper.eq(DictionaryType::getDicTypeCode,dto.getDicTypeCode());
        dictionaryTypeMapper.update(null,lambdaUpdateWrapper);
        // 2. 删除与当前字典类型关联的字典表信息
        LambdaUpdateWrapper<Dictionary> dictionaryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        dictionaryLambdaUpdateWrapper.eq(Dictionary::getIsDeleted,IsDeletedEnum.NO.getFlag());
        dictionaryLambdaUpdateWrapper.eq(Dictionary::getDicTypeCode,dto.getDicTypeCode());
        dictionaryLambdaUpdateWrapper.set(Dictionary::getIsDeleted,IsDeletedEnum.YES.getFlag());
        dictionaryMapper.update(null,dictionaryLambdaUpdateWrapper);
        return "操作成功";
    }

    /**
     * 修改数据字典类型 code不能修改
     * @param dto 入参
     * @return 返回提示信息
     */
    public String updateDictionaryType(DictionaryTypeUpdateDto dto) {
        LambdaUpdateWrapper<DictionaryType> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DictionaryType::getDicTypeId,dto.getDicTypeId());
        lambdaUpdateWrapper.set(DictionaryType::getDicTypeName, dto.getDicTypeName());
        lambdaUpdateWrapper.set(DictionaryType::getDicTypeSort, dto.getDicTypeSort());
        lambdaUpdateWrapper.set(DictionaryType::getDescript, dto.getDescript());
        dictionaryTypeMapper.update(null,lambdaUpdateWrapper);
        return "操作成功";
    }

    public IPage<DictionaryType> selectByPage(DictionaryTypePageDto dto) {
        LambdaQueryWrapper<DictionaryType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(dto.getDicTypeCode()), DictionaryType::getDicTypeCode,dto.getDicTypeCode());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getDicTypeName()), DictionaryType::getDicTypeName,dto.getDicTypeName());
        lambdaQueryWrapper.eq(DictionaryType::getIsDeleted,IsDeletedEnum.NO.getFlag());
        lambdaQueryWrapper.orderByAsc(DictionaryType::getDicTypeSort);
        return this.page(new Page(dto.getPageNo(),dto.getPageSize()),lambdaQueryWrapper);
    }
}
